package com.ruoyi.web.controller.teaching;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.PlatformQKnowledge;
import com.ruoyi.teaching.service.CheckService;
import com.ruoyi.teaching.service.PlatformQKnowledgeService;
import com.ruoyi.teaching.utils.UniversalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王旭
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/platform/knowledge")
@Api(tags = "platform-知识点管理")
@Validated
public class PlatformQKnowledgeController {
    @Autowired
    private PlatformQKnowledgeService platformQKnowledgeService;
    @Autowired
    private CheckService checkService;

    /**
     * 查询知识点列表（发布页面）
     */
//    @PreAuthorize("@ss.hasPermi('platform:knowledge:list')")
    @GetMapping("/listExam")
    @ApiOperation(value = "查询知识点列表（发布页面）", notes = "查询知识点列表（发布页面）")
    public AjaxResult listExam()
    {
        return platformQKnowledgeService.selectPlatformQKnowledgeListInExam();
    }

    /**
     * 查询知识点列表（题库页面）
     */
//    @PreAuthorize("@ss.hasPermi('platform:knowledge:list')")
    @GetMapping("/listQuestion")
    @ApiOperation(value = "查询知识点列表（题库页面）", notes = "查询知识点列表（题库页面）")
    public AjaxResult listQuestion()
    {
        return platformQKnowledgeService.selectPlatformQKnowledgeListInQuestion();
    }

    /**
     * 查询知识点详细信息
     */
//    @PreAuthorize("@ss.hasPermi('platform:knowledge:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询知识点详细信息", notes = "查询知识点详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return platformQKnowledgeService.selectPlatformQKnowledgeById(id);
    }

    /**
     * 新增/修改 知识点
     */
//    @PreAuthorize("@ss.hasPermi('platform:knowledge:add')")
    @Log(title = "新增/修改 知识点", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增/修改 知识点", notes = "新增/修改 知识点")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody PlatformQKnowledge platformQKnowledge)
    {
        List<String> errorMessages = new ArrayList<>();
        errorMessages.addAll(UniversalUtil.validate(platformQKnowledge.getKnowledgeName(),1,50,true,"知识点名称"));
        if (!errorMessages.isEmpty()) {
            return AjaxResult.error(String.join(", ", errorMessages));
        }
        return platformQKnowledgeService.insertPlatformQKnowledge(platformQKnowledge);
    }

    /**
     * 删除知识点
     */
//    @PreAuthorize("@ss.hasPermi('platform:knowledge:remove')")
    @Log(title = "删除知识点", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除知识点", notes = "删除知识点")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            String knownledgeName = checkService.checkQuestionInKnowledge(id);
            if(!knownledgeName.equals("")) {
                return AjaxResult.error("知识点【"+knownledgeName+"】被试题引用，无法删除！请重新选择");
            }
        }
        return platformQKnowledgeService.deletePlatformQKnowledgeByIds(ids);
    }
}

